﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Web.WebSockets;
using Channels;
using Cards;
using Games;
using Rooms;
using Players;

namespace BackEnd
{
    public partial class WebSocketHandler : Microsoft.Web.WebSockets.WebSocketHandler
    {
        /// <summary>
        /// Packet struct
        /// 
        /// ======
        /// Login
        /// L111|account|passowrd
        /// =====
        /// </summary>
        private static WebSocketCollection clients = new WebSocketCollection();
        public static List<WebSocketHandler> ArrayClient = new List<WebSocketHandler>();

        private Player ObjPlayer;
        private bool bJoinChannel;
        private bool bJoinRoom;
        /// <summary>
        /// id channel current
        /// </summary>
        private int idchanneljoin = -1;
        /// <summary>
        /// id room current
        /// </summary>
        private int idroomjoin = -1;
        /// <summary>
        /// 0 in room but not join game
        /// 1 join in room and join game but not ready
        /// 2 ready
        /// </summary>
        private int statusReady = 0;
        private string strUsername;
        private string strPassword;
        
        private bool bLogin;
        public override void OnOpen()
        {
            /*
            this.strUsername = this.WebSocketContext.QueryString["Username"];
            this.strPassword = this.WebSocketContext.QueryString["Password"];
            if (Login(strUsername, strPassword))
            {
                clients.Broadcast("Login");
            }
            else
            {
                clients.Broadcast("ReLogin");
            }*/
            //ArrayClient.Add(this);
            clients.Add(this);
            //clients.Broadcast("You connected.");
            //this.Send("Channels: " + Channel.LstChannels.ToString());
        }
        
        public override void OnMessage(string message)
        {
            string[] Arr = message.Split('|');
            if (Arr[0] == "L111")
            {
                if (ArrayClient.Count > 0)
                {
                    for (int i = 0; i < ArrayClient.Count; i++)
                    {
                        if (ArrayClient[i].ObjPlayer.StrAccount.Equals(Arr[1]))
                        {
                            this.Send("Account this connected, please recheck");
                            return;
                        }
                    }
                }
                if (this.bLogin == false)
                {
                    ObjPlayer = Login(Arr[1], Arr[2]);
                    strUsername = Arr[1];
                    strPassword = Arr[2];
                    if (ObjPlayer != null)
                    {
                        ArrayClient.Add(this);
                        this.bLogin = true;
                        clients.Broadcast(Arr[1] + " connected.");
                        this.Send("Login");
                        this.Send("Please select channel in list");

                        for (int i = 0; i < 10; i++)
                        {
                            this.Send("Channel name: " + Channel.LstChannels[i].StrChannelName + ", Channel ID: " + Channel.LstChannels[i].IChannelID);
                        }
                    }
                    else
                    {
                        this.Send("ReLogin");
                    }
                }
                else
                {
                    // account logged
                }
            }
            else if (Arr[0] == "joinChannel")
            {
                int idChannel = int.Parse(Arr[1]);
                _JoinChannel(idChannel);
            }
            else if (Arr[0] == "outChannel")
            {
                int idChannel = int.Parse(Arr[1]);
                _OutChannel(idChannel);
            }
            else if (Arr[0] == "joinRoom")
            {
                int idRoom = int.Parse(Arr[1]);
                _JoinRoom(idRoom);
            }
            else if (Arr[0] == "outRoom")
            {
                int idRoom = int.Parse(Arr[1]);
                _OutRoom(idRoom);
            }
        }

        public override void OnError()
        {
            base.OnError();
        }

        public override void OnClose()
        {
            if (ArrayClient.Count > 0)
            {
                for (int i = 0; i < ArrayClient.Count; i++)
                {
                    if (this.ObjPlayer != null)
                    {
                        ArrayClient.Remove(this);
                        break;
                    }
                }
            }
            clients.Remove(this);
            clients.Broadcast(string.Format("{0} has gone away.", strUsername));
        }

        Player Login(string account, string password)
        {
            Player ObjPlayer = new Player();
            return ObjPlayer.Login(account, password);
        }
    }
}